//
//  ChatTextCell.m
//  学生汇
//
//  Created by yull on 14/11/10.
//  Copyright (c) 2014年 于博. All rights reserved.
//

#define CHAT_TEXT_CELL_X_SPACE 10
#define CHAT_TEXT_CELL_Y_SPACE 10

#import "ChatTextCell.h"
#import "UIImageView+WebCache.h"

@implementation ChatTextCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentLabel = [[UILabel alloc] initWithFrame:self.chatContentView.bounds];
        self.contentLabel.backgroundColor = [UIColor clearColor];
        self.contentLabel.font = [UIFont systemFontOfSize:16];
        self.contentLabel.textColor = [UIColor blackColor];
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.textAlignment = NSTextAlignmentLeft;
        self.contentLabel.userInteractionEnabled = YES;
        [self.chatContentView addSubview:self.contentLabel];
        
        self.xSpace = 10;
        self.ySpace = 10;
        
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)];
        [self.contentImageView addGestureRecognizer:longPress];
    }
    return self;
}

+ (CGFloat)calculateCellHeightWithText:(NSString *)text
{
    CGSize size = [ChatTextCell contentLabelWidth:text];
    CGFloat height = size.height + 16 * 2 + CHAT_TEXT_CELL_Y_SPACE * 2;
    return height;
}

- (void)drawCellWithChatMsg:(ChatBaseMsg *)chatMsg
{
    self.contentLabel.text = chatMsg.decodeContent;
    
    CGSize size = [ChatTextCell contentLabelWidth:chatMsg.decodeContent];
    
    CGRect rect = self.chatContentView.frame;
    rect.size = size;
    self.chatContentView.frame = rect;
    
    self.contentLabel.frame = self.chatContentView.bounds;
    
    [super drawCellWithChatMsg:chatMsg];
    
    if (chatMsg.roleType == ChatMsgRoleTypeReceive) {
        self.failImageView.left = self.contentImageView.right + 5;
//        self.sendingImageView.left = self.contentImageView.right + 5;
    }else{
        self.failImageView.right = self.contentImageView.left - 5;
//        self.sendingImageView.right = self.contentImageView.left - 5;
    }
}

+ (CGSize)contentLabelWidth:(NSString *)content
{
    CGSize constrainSize = CGSizeMake(180, CGFLOAT_MAX);
    return [content sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:constrainSize];
}

- (void)onLongPress:(UILongPressGestureRecognizer *)pressGesture
{
    if (pressGesture.state == UIGestureRecognizerStateBegan) {
        
        [self becomeFirstResponder];
        
        if ([self.delegate respondsToSelector:@selector(chatTextCellOnLongPress)]) {
            [self.delegate chatTextCellOnLongPress];
        }
        
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        
        UIMenuItem *item = [[UIMenuItem alloc] initWithTitle:@"拷贝" action:@selector(onCopy:)];
        [menuController setMenuItems:[NSArray arrayWithObject:item]];
        
        [menuController setTargetRect:self.contentImageView.frame inView:self.contentView];
        [menuController setMenuVisible:YES animated:YES];
    }
}

- (void)onCopy:(id)sender
{
    [UIPasteboard generalPasteboard].string = self.chatMsg.content;
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(onCopy:)) {
        return YES;
    }
    return NO;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
